package com.gxa.worker.recycling.controller;

import com.gxa.core.recycling.anno.NeedToken;
import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.User;
import com.gxa.worker.recycling.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;

/**
 * @author ：looker
 * @date ：Created in 2021/12/2 10:29
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
@Api("回收员前台--用户业务处理接口")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 杨述波
     * @param id
     * @param likeWord
     * @return
     * @throws BusinessException
     */
    @ApiOperation("回收员隶属小区用户查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "回收员id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "likeWord", value = "姓名/电话模糊词,为空则全查",required = false,dataType = "string")
    })
    @NeedToken
    @PostMapping("/getAllUser")
    public Map getAllUserByPosition(Long id, String likeWord) throws BusinessException {
        if (id==null) {
            throw new BusinessException(ExceptionCode.NO_PEOPLE);
        }
        List<Map<String, Object>> userByPositionOrPhone = userService.getUserByPositionOrPhone(id, likeWord);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(userByPositionOrPhone);
        return jsonResult.getDataMap();
    }


    /**
     * 杨述波
     * @param id
     * @return
     * @throws BusinessException
     */
    @ApiOperation("用户信息及回收废品与兑换物品小定单查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    })
    @NeedToken
    @PostMapping("/getOrdersById")
    public Map getOrdersById(Long id) throws BusinessException {
        if (id==null) {
            throw new BusinessException(ExceptionCode.NO_PEOPLE);
        }
        Map ordersById = userService.getOrdersById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(ordersById);
        return jsonResult.getDataMap();
    }


    /**
     * 杨述波
     * @param id
     * @return
     * @throws BusinessException
     */
    @ApiOperation("用户信息全查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    })
    @NeedToken
    @PostMapping("/selectById")
    public Map selectById(Long id) throws BusinessException {
        if (id==null) {
            throw new BusinessException(ExceptionCode.NO_PEOPLE);
        }
        Map userById = userService.getUserById(id);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(userById);
        return jsonResult.getDataMap();
    }

    /**
     * 廖泓宇
     * 新建临时用户
     */
    @PostMapping("/create")
    @ApiOperation("创建交废人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "用户电话", required = true, dataType = "String"),
            @ApiImplicitParam(name = "provinces", value = "省市区", required = true, dataType = "String"),
            @ApiImplicitParam(name = "quartersId", value = "小区id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "location", value = "详细地址", required = true, dataType = "String")
    })
    @NeedToken
    public Map createUser(@RequestBody User user){
        userService.createUser(user);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

}
